<?php

/* 
 * 来源
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class storySourceController extends AppController
{
    public function __construct() {
        parent::__construct();
        $this->modelObj = new storySourceModel();
        $this->log_type = '来源管理';
        $this->cookie_key = 'source_cookie';
    }
    public function view(){
        $this->title = '来源';
        $this->_get_data();
        $this->display('source_list');
    }
    public function query(){
        $this->_get_data();
        $this->ret['status'] = 0;
        $this->ret['content'] = $this->fetch('source_list');
        exit(json_encode($this->ret));
    }

    public function create(){
        $this->create_token();
        $this->get_cookie();
        $this->title = '添加来源';
        $this->display('sourece_create');
    }
    public function new_create(){
        $this->check_token();
        $data = array(
            'sname' => trim($_POST['sname']),
            'url' => trim($_POST['url']),
            'in_time' => p_getTime(),
        );
        $this->set_cookie($data);
        if(empty($data['sname'])) {
            $this->show_msg('名称不能为空');
        }
        if(empty($data['url'])) {
            $this->show_msg('URL不能为空');
        }        
        $where = array(
            'sname' => $data['sname'],
        );
        if(storySourceModel::load()->getRow(null, $where)) {
            $this->show_msg('名称重复');
        }
        $bool = storySourceModel::load()->insert($data);
        if($bool) {
            $this->show_msg('添加成功', 'c=storysource&m=view');
        } else {
            $this->show_msg('添加失败');
        }
    }
    public function del(){
        $sid = $_POST['id'];
        if($sid > 0) {
            if(storyModuleModel::load()->getSid($sid)) {
                echo $this->setReturn(100, '不能删除，因为在模块里含有数据');exit;
            } else {
                $where = array('sid' => $sid);
                $bool = storySourceModel::load()->delete($where);
                if($bool) {
                    exit($this->setReturn(0, 'ok'));
                }
            }
        }
        echo $this->setReturn(101, '删除失败');
    }
    /**
     * 来源获取数据
     */
    private function _get_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;  
        $filter['sname'] = isset($_POST['sname']) ? $_POST['sname'] : '';
        if(!empty($filter['user_name'])) {
            $where .= ' and instr(sname, ' . "'{$filter['sname']}')";
        }
        $key = storySourceModel::load()->keyFields;
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $key;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'DESC';
        $pageObj = new pageModel('story_source', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);
        $list = $data['pageData'];
        if($list) {
            foreach ($list as $key => $row) {                
                $data['pageData'][$key]['id'] = $key + 1;
                $data['pageData'][$key]['ssid'] = $row['sid'];
                $data['pageData'][$key]['in_time_format'] = publicController::load()->friendlyDate($row['in_time']);               
                $data['pageData'][$key]['is_show'] = $row['is_show'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';  
            }
        }
        $this->assign('sort_' . $sort_by, $data['sort_img']);
        $this->assign('data', $data);
    }
}

